package com.mysterymall.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.BaseEnum;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.OrderInfoMapper;
import com.mysterymall.Mapper.OtherMapper;
import com.mysterymall.Mapper.UserMapper;
import com.mysterymall.Model.Dto.CustomerServiceDto;
import com.mysterymall.Model.Entity.OrderInfo;
import com.mysterymall.Model.Entity.Other;
import com.mysterymall.Model.Entity.User;
import com.mysterymall.Service.MarketingService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2024/01/05  14:16
 */
@Service
public class MarketingServiceImpl implements MarketingService {


    private final UserMapper userMapper;

    private final OrderInfoMapper orderInfoMapper;


    private final OtherMapper otherMapper;

    public MarketingServiceImpl(UserMapper userMapper, OrderInfoMapper orderInfoMapper, OtherMapper otherMapper) {
        this.userMapper = userMapper;
        this.orderInfoMapper = orderInfoMapper;
        this.otherMapper = otherMapper;
    }


    @Override
    public Result getMarketingData(String startTime, String endTime) {


        Map<String, Object> map = new HashMap<>();

        List<Long> newUserList = new ArrayList<>();

        List<Instant> startTimes = new ArrayList<>();
        List<Instant> endTimes = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d HH:mm:ss");
        LocalDateTime startDate = LocalDateTime.parse(startTime, formatter);
        LocalDateTime endDate = LocalDateTime.parse(endTime, formatter);

        LocalDateTime currentDateTime = startDate;

        while (!currentDateTime.isAfter(endDate)) {
            Instant startInstant = currentDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Instant endInstant = currentDateTime.withHour(23).withMinute(59).withSecond(59)
                    .atZone(ZoneId.systemDefault()).toInstant();

            startTimes.add(startInstant);
            endTimes.add(endInstant);
            currentDateTime = currentDateTime.plusDays(1);
        }
        startTimes.forEach(System.out::println);
        endTimes.forEach(System.out::println);

        List<String> timeList = new ArrayList<>();

        // 我需要 2020-12-01 这种格式的时间
        for (Instant instant : startTimes) {
            LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
            String time = localDate.toString();
            timeList.add(time);
        }

        map.put("timeList", timeList);


        // 查询全部用户人数
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getRoleId, BaseEnum.User.getCode());

        long UserSum = userMapper.selectCount(lambdaQueryWrapper);

        map.put("userSum", UserSum);

        long totalUser = 0;
        // 查询新增用户数
        for (int i = 0; i < startTimes.size(); i++) {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.between(User::getCreateTime, startTimes.get(i), endTimes.get(i));
            long count = userMapper.selectCount(userLambdaQueryWrapper);
            newUserList.add(count);
            totalUser += count;
        }
        map.put("newUserList", newUserList);
        map.put("totalUser", totalUser);


        // 查询总支付金额
        List<BigDecimal> totalPayAmountList = new ArrayList<>();
        BigDecimal totalPayAmountSum = new BigDecimal(0);
        List<String> orderType = new ArrayList<>();
        orderType.add(BaseEnum.Goods_Type_Game_Currency.getCode());
        orderType.add(BaseEnum.Goods_Type_Goods_Attribute.getCode());

        List<String> orderInfoStatus = new ArrayList<>();
        orderInfoStatus.add(BaseEnum.Pending_Payment.getCode());
        orderInfoStatus.add(BaseEnum.Cancelled.getCode());
        orderInfoStatus.add(BaseEnum.Refunded.getCode());
        orderInfoStatus.add(BaseEnum.Abnormal.getCode());
        for (int i = 0; i < startTimes.size(); i++) {
            LambdaQueryWrapper<OrderInfo> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.between(OrderInfo::getCreateTime, startTimes.get(i), endTimes.get(i));
            userLambdaQueryWrapper.in(OrderInfo::getGoodsType, orderType);
            userLambdaQueryWrapper.notIn(OrderInfo::getOrderStatus, orderInfoStatus);
            BigDecimal totalPayAmount = new BigDecimal(0);
            for (OrderInfo orderInfo : orderInfoMapper.selectList(userLambdaQueryWrapper)) {
                totalPayAmount = totalPayAmount.add(orderInfo.getGoodsAmount());
            }
            totalPayAmountList.add(totalPayAmount);
            totalPayAmountSum = totalPayAmountSum.add(totalPayAmount);
        }

        map.put("totalPayAmountList", totalPayAmountList);
        map.put("totalPayAmountSum", totalPayAmountSum);


        return new Result(HttpStatus.SUCCESS, "message.querySuccess", map);
    }

    @Override
    public Result addCustomerService(CustomerServiceDto customerServiceDto) {
        Other other = new Other();
        other.setContent(customerServiceDto.getContent());
        other.setTitle(customerServiceDto.getTitle());
        other.setNotes(customerServiceDto.getImages());
        other.setType(BaseEnum.Other_Type_Customer_Service.getCode());
        otherMapper.insert(other);
        return new Result(HttpStatus.SUCCESS, "message.addSuccess");
    }

    @Override
    public Result addPlayingMethod(CustomerServiceDto customerServiceDto) {


        // 删除原来的玩法介绍
        LambdaQueryWrapper<Other> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Play_Introduction.getCode());
        otherMapper.delete(lambdaQueryWrapper);



        Other other = new Other();
        other.setContent(customerServiceDto.getContent());
        other.setNotes(customerServiceDto.getImages());
        other.setType(BaseEnum.Other_Type_Play_Introduction.getCode());
        otherMapper.insert(other);
        return new Result(HttpStatus.SUCCESS, "message.addSuccess");
    }

    @Override
    public Result getCustomerService() {
        LambdaQueryWrapper<Other> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Customer_Service.getCode());
        List<Other> otherList = otherMapper.selectList(lambdaQueryWrapper);


        lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Play_Introduction.getCode());

        Other other = otherMapper.selectOne(lambdaQueryWrapper);




        Map<String,Object> map = new HashMap<>();

        map.put("playingMethod",other);

        map.put("customerServiceList",otherList);

        lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Logo.getCode());

        Other other1 = otherMapper.selectOne(lambdaQueryWrapper);

        map.put("logo",other1);

        return new Result(HttpStatus.SUCCESS, "message.querySuccess", map);
    }

    @Override
    public Result delete(String id) {
        otherMapper.deleteById(id);
        return new Result(HttpStatus.SUCCESS, "message.deleteSuccess");
    }

    @Override
    public Result getCustomerServiceUser(HttpServletRequest request) {

        LambdaQueryWrapper<Other> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Customer_Service.getCode());
        List<Other> otherList = new ArrayList<>();


        for (Other other : otherMapper.selectList(lambdaQueryWrapper)) {
            switch ((String) request.getAttribute("Lang")){
                case "zh":
                    other.setTitle(other.getTitle().split("&")[0]);
                    break;
                case "ms":
                    other.setTitle(other.getTitle().split("&")[1]);
                    break;
                case "en":
                    other.setTitle(other.getTitle().split("&")[2]);
                    break;
            }
            otherList.add(other);
        }


        lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Play_Introduction.getCode());

        Other other = otherMapper.selectOne(lambdaQueryWrapper);

        Map<String,Object> map = new HashMap<>();

        map.put("playingMethod",other);

        map.put("customerServiceList",otherList);


        lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Logo.getCode());

        Other other1 = otherMapper.selectOne(lambdaQueryWrapper);

        map.put("logo",other1);

        return new Result(HttpStatus.SUCCESS, "message.querySuccess", map);
    }

    @Override
    public Result addLogo(CustomerServiceDto customerServiceDto) {
        // 删除原来的logo
        LambdaQueryWrapper<Other> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Other::getType, BaseEnum.Other_Type_Logo.getCode());

        otherMapper.delete(lambdaQueryWrapper);

        Other other = new Other();
        other.setContent("");
        other.setNotes(customerServiceDto.getImages());
        other.setType(BaseEnum.Other_Type_Logo.getCode());
        otherMapper.insert(other);
        return new Result(HttpStatus.SUCCESS, "message.addSuccess");
    }


}
